<?php
/**注意此文档为UTF-8的编码.
 * Created on 2010-5-17
 *
 * @author EXT <isrn@qq.com>
 *
 */
require_once '../../includes/global.php';


function loadUrl($title,$url,$width,$height,$mask,$id=''){
$objResponse = new xajaxResponse();
$id=$id==''?'addWindow':$id;
$str2="	var addwin =  new Ext.Window({
				id :'".$id."',
				title :'".$title."',
				iconCls:'plugin_edit',
				closable:true,
				closeAction :'close',
				plain:true,
				width:".$width.",
				height:".$height.",
				html:'<iframe scrolling=\"auto\" id=\"adel1828\" name=\"adel1828\" frameborder=\"0\" width=\"100%\" height=\"100%\" src=\"".$url."\"></iframe>'
			});
	addwin.show();
	";
$str2=str_replace("\r","",$str2);
$str2=str_replace("\n","",$str2);
$str2=str_replace("\t","",$str2);
	$objResponse->script($str2);
	return $objResponse;
}

/*function loadUrl($title,$url,$width,$height,$mask,$id=''){
$objResponse = new xajaxResponse();
$id=$id==''?'addWindow':$id;
$str2="	var addwin =  new Ext.Window({
				id :'".$id."',
				title :'".$title."',
				iconCls:'plugin_edit',
				closable:true,
				closeAction :'close',
				plain:true,
				width:".$width.",
				height:".$height.",
				autoLoad:{url:'".$url."',nocache:true,scripts:true}
			});
	addwin.show();
	";
$str2=str_replace("\r","",$str2);
$str2=str_replace("\n","",$str2);
$str2=str_replace("\t","",$str2);
	$objResponse->script($str2);
	return $objResponse;
}*/

function items_config($tree_id,$field,$add=false){
	$objResponse = new xajaxResponse();
	$action = $add?'add':'save';
	$dbcfg=Sys::Gp(array('dbcfg','sevenel'));
	$p_id = "p_id:$tree_id";
	if(!$add){
			$field_data=Dgd(array(
				  'db'=>'d_sevenel',
				  'cd'=>"parent_tree = $tree_id and field_name ='$field' and is_shield = 0",
				  'tn'=>"fieldmod",
				  'limit'=>'1'
			));
			$config = json_decode($field_data['config'],true);
			$c_id = "{$field_data['id']}";
			$type = $field_data['type'];
	}else{
			$c_id = '0';
			$type = $field;
						$tree=Dgd(array(
						  'db'=>'d_sevenel',
						  'cd'=>"id = $tree_id",
						  'tn'=>"tree",
						  'limit'=>'1'
						));
						$tabname = gettabname($tree['table'],$dbcfg['prefix']);
						$field_=Dgd(array(
							'db'=>'d_sevenel',
							'cd'=>"parent_tree = $tree_id",
							'tn'=>"fieldmod",
							'column'=>'field_name'
						));
				 		$fullfields = Dgd(array(
						  'db'=>'d_sevenel',
						  'sql'=>"SHOW FULL FIELDS FROM ".$tabname
						));
						$doNotCreate=array('id','del','lg','gid','sortid','cataid');
						$existfields = array();
						if($field_ !=''){
						foreach ($field_ as $key=>$value){
								$existfields[] = $value['field_name'];
				    		}
						}
				    	foreach($fullfields as $arrow=>$data){
								if(!in_array($data['Field'],$existfields)&&!in_array($data['Field'],$doNotCreate)){
									$store_[]= "['{$data['Field']}']";
								}
							}
						if($store_==''){
							$str = "Ext.Msg.alert('提示','没有可用的字段了!')";
							$objResponse->script($str);
							return $objResponse;
						}
				    	foreach($store_ as $array=>$f){
				    		$store .= $f;
				    		if($array < count($store_)-1){
								$store .= ',';
				    		}
				    	}
						$heigh = '+100';
						$items = "{
						xtype:'combo',
						id : 'combobox',
						name : 'field',
						width:130,
						fieldLabel:'使用字段',
						store:new Ext.data.SimpleStore({
						fields:['value'],
						data:[$store]
						}),
						mode:'local',
				        displayField:'value',
				        triggerAction:'all'
						},
					";
						$p_id=$tree_id;
						$type_="type:'$type',";
	}
	require_once '../form/items_config.php';
	foreach($type_array as $arrow => $value){
			if($value['name'] == $type){
				$elment = $value;
				}
			}
	switch($type){
			case 'checkboxgroup':
			case 'radiogroup':
			case 'combo':
				$str="	var addwin =  new Ext.Window({
				id :'elment_config',
				title :'组件配置',
				iconCls:'add',
				closable:true,
				closeAction :'close',
				plain:true,
				width:{$elment['width']},
				height:{$elment['height']},
				html:'<iframe scrolling=\"auto\"  frameborder=\"0\" width=\"100%\" height=\"100%\" src=\"form/form_config.php?op=form_component&component=$type&c_id=$c_id&p_id=$p_id\"></iframe>'
			});
				addwin.show();
				";
			break;
			default:
			if($elment['config'] != ''){
				foreach($elment['config'] as $key_ => $value_){
						$config!=''&&$v="value:'{$config[$value_['name']]}',";
						if($type == 'password'&&$config[$value_['name']]=='on'){
							$v = '"checked":true,';
						}
						$items.="{xtype:'{$value_['type']}',
										name:'{$value_['name']}',
										".$v."
										fieldLabel:'{$value_['lable']}'}";
							 			$el_config .= $value_['name'];
						if($key<count($elment['config'])-1){
										$items.=',';
										$el_config.=',';
								}
					}
				}
					$el_config = ",param:'".$el_config."'";
					$bbar = ",bbar:['->',{xtype:'button',text:'保存',handler:function(){
							Ext.getCmp('elment_form').form.doAction('submit',{
									url:'form/form_config.php',
									method:'POST',
									params:{require:'items_config_$action','pid':{$p_id},{$type_}id:'$c_id'$el_config},
									waitMsg:'正在提交....',
									success:function(form,action){
											if (action.result.msg=='ok') {
												Ext.Msg.alert('数据编辑','成功！');
												addwin.close();
											} else {
												Ext.Msg.alert('数据编辑',action.result.msg);
											}
									 }
									 ,
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
								});
						}}]";
					$str="var addwin =  new Ext.Window({
							id :'elment_config',
							title :'组件配置',
							closable:true,
							closeAction :'close',
							width:{$elment['width']},
							height:{$elment['height']}$heigh,
							items:[{
								xtype:'form',
								id:'elment_form',
								border:false,
								frame:true,
								style:'padding:10px',
								items:[".$items."]

							}]
							".$bbar."
						});
							addwin.show();
						";
					}

	$str=str_replace("\r","",$str);
	$str=str_replace("\n","",$str);
	$str=str_replace("\t","",$str);
	$objResponse->script($str);
	return $objResponse;
}
$xajax = new xajax();
$xajax->registerFunction("loadUrl");
$xajax->registerFunction("items_config");
$xajax->processRequest();
?>